#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite3.h>

static char* id;	// 获取id放入此处
static char* temp;	// 获取temp放入此处
static char* humi;	// 获取humi放入此处

static int myfunc(void *p, int argc, char **argv, char **argvv)                      
{      
    int i;
    *(int *)p= 0;                                                              
    for(i =0; i < argc; i++)                                                  
        printf("%s", argv[i] ? argv[i] : "NULL");     
    printf("\n");        
	return 0;
}


int main()
{
	sqlite3 *db;
	char *err =0;
	int ret_sqlite =0;// 记录表的长度，单次执行时有效
	int empty =1;

	printf("Content-type: text/html\n\n" );
	printf("<html>\n");
	printf("<head>\n");
	printf("<title>CGI 0utput</title>\n");
	printf("<head>");
	printf("<meta http-equiv=\"refresh\" content=\"1\">");
	printf("</head>");
	printf("</head>\n");
	printf("<body onload=\"JavaScript:AutoRefresh(1000);\">");
	printf("<center>");
	printf("<h1> 小青蛙智慧农场控制端 </h1>");
	
	// 与数据库建立连接，查看数据库内容
	ret_sqlite = sqlite3_open("/var/www/xuedao", &db);
	if(ret_sqlite != SQLITE_OK)
	{
		printf("sqlite3 open error\n");
		exit(1);
	}

	printf("<table border = \"1\" width='400px' height='400px'>");
	printf("<tr>");
	printf("<td align = \"center\" bgcolor = \"#AE4A74\">");
	printf("<font size = \"6\">序号：</font>");
	printf("</td>");
	printf("<td align = \"center\" bgcolor = \"#AE4A74\">");
	printf("<font size = \"6\" id = \"_id\">");
	ret_sqlite = sqlite3_exec(db,"select id from hb ORDER BY id DESC LIMIT 1", myfunc, &empty, &err);
	printf("</font>");
	printf("</td>");
	printf("</tr>");

	printf("<tr>");
	printf("<td align = \"center\" bgcolor = \"#B66A8F\">");
	printf("<font size = \"6\">温度：</font>");
	printf("</td>");
	printf("<td align = \"center\" bgcolor =\"#B66A8F\">");
	printf("<font size = \"6\">");
	printf("<div id = \"temp\">");
	ret_sqlite = sqlite3_exec(db,"select temp from hb ORDER BY id DESC LIMIT 1" , myfunc, &empty, &err);
	printf("</div>");
	printf("</font>");
	printf("</td>");
	printf("</tr>");

	printf("<tr>");
	printf("<td align = \"center\" bgcolor = \"#C291A1\">");
	printf("<font size = \"6\">湿度：</font>");
	printf("</td>");
	printf("<td align = \"center\" bgcolor = \"#C291A1\">");
	printf("<font size = \"6\" id = \"humi\">");
	ret_sqlite = sqlite3_exec(db,"select humi from hb ORDER BY id DESC LIMIT 1" , myfunc, &empty, &err);
	printf("</font>");
	printf("</td>");
	printf("</tr>");

	printf("<tr>");
	printf("<td align = \"center\" bgcolor = \"#E8C6DD\">");
	printf("<font size = \"6\">");
	printf("<a href = \"HB_CGI_open.cgi\">打开</a>");	
	printf("</font>");
	printf("</td>");
	printf("<td align = \"center\" bgcolor = \"#E8C6DD\">");
	printf("<font size = \"6\">");
	printf("<a href = \"HB_CGI_close.cgi\">关闭</a>");	
	printf("</font>");
	printf("</td>");
	printf("</tr>");

	printf("</table>");

	printf("<p></p>");
	
	//printf("<a href=\"javascript:location.reload(true)\">刷新页面</a>");
    printf("<div id=\"time\"></div>");
	printf("</center>");
    
	printf("<script>");
        printf("function showTime() {");
            printf("var date = new Date();");

            // 年月日
            printf("var year = date.getFullYear();");
            printf("var month = date.getMonth() + 1;");
            printf("var day = date.getDate();");

            // 时分秒
            printf("var hour = date.getHours();");
            printf("var minute = date.getMinutes();");
            printf("var second = date.getSeconds();");

            // 实时显示
            printf("var element = document.getElementById('time');");
            printf("element.innerHTML = '<h1>' + year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;");
        printf("}");

        printf("setInterval('showTime()', 1);");

    printf("</script>");
	printf("</body>");

	printf("</html>\n");
	return 0;
}
